<?php

/*
 * Fresns Control Panel (Standalone)
 * Copyright (C) 2021-Present 刘鑫
 * Released under the MIT License.
 */

namespace App\Plugins\FcpApi\TweetNotifyTemplates;

use App\Base\Controllers\BaseAdminController;
use App\Http\Center\Common\ValidateService;
use App\Plugins\FcpApi\Base\TweetBaseController;
use App\Plugins\FcpApi\TweetConfigs\TweetConfigs;
use App\Plugins\FcpApi\TweetConfigs\TweetConfigsConfig;
use App\Plugins\FcpApi\TweetConfigs\TweetConfigService;
use Illuminate\Http\Request;

class FsControllerAdmin extends TweetBaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->service = new FsService();
    }

    public function index(Request $request)
    {
        // dd($newArr);
        $idArr = TweetConfigs::where('item_tag', FsConfig::VERIFYCODE_TEMPLATE)->orderby('id', 'asc')->pluck('id')->toArray();
        $tweetConfigService = new TweetConfigService();
        $request->offsetSet('ids', implode(',', $idArr));
        $tweetConfigService->setResource(TweetConfigsResource::class);
        $data = $tweetConfigService->searchData();
        unset($data['common']['areasOption']);
        unset($data['common']['dateOption']);
        unset($data['common']['languageOption']);
        $data['common']['channel'] = FsConfig::PUSH_CHANNEL;
        $this->success($data);
    }

    public function update(Request $request)
    {
        $table = TweetConfigsConfig::CFG_TABLE;
        $rule = [
            'id'    => "required|exists:{$table},id",
        ];
        ValidateService::validateRule($request, $rule);
        $id = $request->input('id');
        $smsStatus = $request->input('smsStatus', false);
        $emailStatus = $request->input('emailStatus', false);
        $emailTemplate = $request->input('emailTemplate', '');
        $smsTemplate = $request->input('smsTemplate', '');
        $emailTemplate_decode = json_decode($emailTemplate, true);
        $smsTemplate_decode = json_decode($smsTemplate, true);
        $json = [
            [
                'type' => 'email',
                'isEnable' => $emailStatus,
                'template' => $emailTemplate_decode,
            ],
            [
                'type' => 'sms',
                'isEnable' => $smsStatus,
                'template' => $smsTemplate_decode,
            ],

        ];
        TweetConfigs::where('id', $id)->update(['item_value'=>$json]);
        $this->success();
    }
    // public function store(Request $request){

    //     $arr = [
    //         'zh-Hans' => [
    //             'templateDesc' => "模板用途介绍",
    //             'variableDesc' => [
    //                 ['name' => "{sitename}",'desc'=>"站点名称变量名"],
    //                 ['name' => "{name}",'desc'=>"成员名变量名"],
    //                 ['name' => "{code}",'desc'=>"验证码"],
    //                 ['name' => "{time}",'desc'=>"当前时间"],
    //             ],
    //         ],
    //         'zh-Hant' => [
    //             'templateDesc' => "模板用途介绍",
    //             'variableDesc' => [
    //                 ['name' => "{sitename}",'desc'=>"站点名称变量名"],
    //                 ['name' => "{name}",'desc'=>"成员名变量名"],
    //                 ['name' => "{code}",'desc'=>"验证码"],
    //                 ['name' => "{time}",'desc'=>"当前时间"],
    //             ],
    //         ],
    //         'en' => [
    //             'templateDesc' => "模板用途介绍",
    //             'variableDesc' => [
    //                 ['name' => "{sitename}",'desc'=>"站点名称变量名"],
    //                 ['name' => "{name}",'desc'=>"成员名变量名"],
    //                 ['name' => "{code}",'desc'=>"验证码"],
    //                 ['name' => "{time}",'desc'=>"当前时间"],
    //             ],
    //         ],
    //     ];
    //     // dump($arr);
    //     // dd(json_encode($arr));
    //     $template = json_decode($request->input('template'),true);
    //     $variable_desc = json_decode($request->input('variable_desc'),true);
    //     dump($template);
    //     dd($variable_desc);
    // }

    // 验证规则
    public function rules($ruleType)
    {
        $rule = [];

        $config = new FsConfig($this->service->getTable());

        switch ($ruleType) {
            case FsConfig::RULE_STORE:
                $rule = $config->storeRule();
                break;

            case FsConfig::RULE_UPDATE:
                $rule = $config->updateRule();
                break;

            case FsConfig::RULE_DESTROY:
                $rule = $config->destroyRule();
                break;

            case FsConfig::RULE_DETAIL:
                $rule = $config->detailRule();
                break;
        }

        return $rule;
    }
}
